package com.zhukaoguan.boot11.controller;

import com.zhukaoguan.boot11.entity.Banner;
import com.zhukaoguan.boot11.mapper.BannerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
public class BannerController {
    @Autowired(required = false)
    BannerMapper mapper;

    @RequestMapping("/banner/select")
    public List<Banner> select(){
        System.out.println(mapper.select());
        return mapper.select();
    }
    @RequestMapping("/banner/delete")
    public void delete(int id){
        mapper.delete(id);
    }
    @RequestMapping("/banner/insert")
    public int insert(MultipartFile picFile) throws IOException {
        //原始文件名
        String fileName = picFile.getOriginalFilename();
        String suffix =
                fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID()+suffix;
        //文件夹路径
        String dirPath = "d:/upload/vrd";
        File dirFile = new File(dirPath);
        if (!dirFile.exists()){
            dirFile.mkdirs();
        }
        String filePath = dirPath+"/"+fileName;
        //保存文件到上面的路径
        picFile.transferTo(new File(filePath));
        //创建Banner对象 并把图片路径添加到banner对象中
       Banner banner = new Banner();
        banner.setUrl("/"+fileName);
        return mapper.insert(banner);//保存到数据库中
    }
}
